/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Common.Constants;
import Model.Dto.UserAccount;
import Model.UserAccountModel;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Quang Nhat
 */
public class Admin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            String action = request.getParameter("btnAction");
            request.setCharacterEncoding("UTF-8");
            String username = request.getParameter("txtUsername");
            String password = request.getParameter("txtPassword");
            String fullname = request.getParameter("txtFullname");
            if (fullname != null) {
                fullname = new String(fullname.getBytes("8859_1"), "UTF8");
            }
            boolean admin = request.getParameter("chkAdmin") != null;
            boolean accountant = request.getParameter("chkAccountant") != null;
            boolean active = request.getParameter("chkActive") != null;
            UserAccount acc = new UserAccount(username, admin, accountant, fullname, active);
            if ("Update".equalsIgnoreCase(action)) {

                HttpSession session = request.getSession(false);
                UserAccount currUser = (UserAccount) session.getAttribute(Constants.SESSION_USER);
                if (username.equals(currUser.getUsername())) {
                    acc = new UserAccount(username, true, accountant, fullname, true);
                }

                String message = UserAccountModel.validate(acc);
                if (message != null) {
                    request.setAttribute(Constants.REQ_MESSAGE, message);
                } else {
                    UserAccountModel.updateUserAccount(acc);
                    if (password != null && password.length() > 0) {
                        UserAccountModel.setUserAccountPassword(username, password);
                    }
                }
            } else if ("Add".equalsIgnoreCase(action)) {
                String message = UserAccountModel.validate(acc);
                if (password == null || password.length() == 0) {
                    if (message != null) {
                        message += "Password cannot be empty.";
                    } else {
                        message = "Password cannot be empty.";
                    }
                }
                if (message != null) {
                    request.setAttribute(Constants.REQ_MESSAGE, message);
                } else {
                    UserAccountModel.addUserAccount(acc, password);
                }
            }
            List<UserAccount> accs = UserAccountModel.getAllUserAccount();
            request.setAttribute("accs", accs);
            RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/useraccounts/listuseraccounts.jsp");
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
